package org.cagiobox.contactfinder;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
 

public class HttpClient {

	static Logger log = Logger.getLogger(HttpClient.class.getName());

	public String httpGet(String url) throws Exception {
		
		String htmlOut=null;
		
        CloseableHttpClient httpclient = HttpClients.createDefault();
        
        try {
            HttpGet httpget = new HttpGet(url);

            // Execute HTTP request
            log.debug("executing request:" + httpget.getURI());

            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                log.debug("----------------------------------------");
                log.debug(response.getStatusLine());
                log.debug("----------------------------------------");

                // Get hold of the response entity
                HttpEntity entity = response.getEntity();

                if (entity != null) {
                    entity = new BufferedHttpEntity(entity);
                }
                
                // If the response does not enclose an entity, there is no need
                // to bother about connection release
                if (entity != null) {
                	htmlOut = EntityUtils.toString(entity);
                	log.debug(htmlOut);
                }
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
        
        return htmlOut;
    }	
	
	
	public  void test() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpget = new HttpGet("http://www.apache.org/");

            // Execute HTTP request
            log.info("executing request " + httpget.getURI());
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                log.info("----------------------------------------");
                log.info(response.getStatusLine());
                log.info("----------------------------------------");

                // Get hold of the response entity
                HttpEntity entity = response.getEntity();

                // If the response does not enclose an entity, there is no need
                // to bother about connection release
                if (entity != null) {
                    InputStream instream = entity.getContent();
                    try {
                        instream.read();
                        // do something useful with the response
                    } catch (IOException ex) {
                        // In case of an IOException the connection will be released
                        // back to the connection manager automatically
                        throw ex;
                    } finally {
                        // Closing the input stream will trigger connection release
                        instream.close();
                    }
                }
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

}
